/**
 * 
 */
package com.swd2010.mobileSearch.network;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import android.widget.Toast;


/**
 * @author haui
 *
 */
public class PersonSearchAutoCompleteWebservice {

	
	//Haui
	
	private static final String classNameForLog = "PersonSearchAutoCompleteWebservice";
    


	
	
	public static ArrayList<String> autocomplete(URL url) {
		
		
		
	    ArrayList<String> resultList = new ArrayList<String>();
	    
	    HttpURLConnection conn = null;
	    StringBuilder jsonResults = new StringBuilder();

	    
	    
	    try {
	    	
	        
	        //URL url = new URL(URLEncoder.encode(completeUrlString, "utf8"));
	    	//URL url = new URL(completeUrlString);
	    	
	        conn = (HttpURLConnection) url.openConnection();
	        InputStreamReader in = new InputStreamReader(conn.getInputStream());
	        
	        
	        // Load the results into a StringBuilder
	        int read;
	        
	        char[] buff = new char[1024];
	        
	        while ((read = in.read(buff)) != -1) {
	            jsonResults.append(buff, 0, read);
	        }
	        
	    } catch (MalformedURLException e) {
	    	
	        Log.e(classNameForLog, "Error: ", e);
	        return resultList;
	        
	    } catch (IOException e) {
	    	
	        Log.e(classNameForLog, "Error: ", e);
	        return resultList;
	        
	    } finally {
	    	
	        if (conn != null) {
	            conn.disconnect();
	        }
	        
	    }

	    try {
	    	
	    	
	    	if ( jsonResults.toString().equalsIgnoreCase("NULL") )
	    		return resultList;
	    	
	    	
	        // Create a JSON object hierarchy from the results
	        JSONObject jsonObj = new JSONObject(jsonResults.toString());
	        JSONArray predsJsonArray = null;

	        
	        try{
	        	
	        	predsJsonArray = jsonObj.getJSONArray("personBean");
	        	
	        }catch(Exception e){
	        	
	        	
	        }
	        
	        
	        
	        // Extract the Place descriptions from the results
	        //resultList = new ArrayList<String>(predsJsonArray.length());
	        
	        if ( predsJsonArray != null ){
	        	
	        	for (int i = 0; i < predsJsonArray.length(); i++) {

	        		resultList.add(predsJsonArray.getJSONObject(i).getString("fullNameWithDegress"));
	        	}
	        	
	        }
	        else{
	        	
	        	jsonObj = jsonObj.getJSONObject("personBean");
	        	
	        	resultList.add(jsonObj.getString("fullNameWithDegress"));
	        	
	        }
	        
	        
	    } catch (JSONException e) {
	    	
	    	
	        Log.e(classNameForLog, "Cannot process JSON results" + jsonResults.toString() + " " + e);
	        
	        
	    }
	    
	    return resultList;
	
	}	
	
	
}
